package cn.iocoder.yudao.module.system.repository.mybatis.dao.notify;

import cn.iocoder.yudao.common.pojo.PageResult;
import cn.iocoder.yudao.core.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.core.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.core.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.system.repository.mybatis.domain.notify.NotifyMessage;
import cn.iocoder.yudao.module.system.vo.notify.message.NotifyMessageMyPageReqVO;
import cn.iocoder.yudao.module.system.vo.notify.message.NotifyMessagePageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface NotifyMessageMapper extends BaseMapperX<NotifyMessage> {

    default PageResult<NotifyMessage> selectPage(NotifyMessagePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<NotifyMessage>()
                .eqIfPresent(NotifyMessage::getUserId, reqVO.getUserId())
                .eqIfPresent(NotifyMessage::getUserType, reqVO.getUserType())
                .likeIfPresent(NotifyMessage::getTemplateCode, reqVO.getTemplateCode())
                .eqIfPresent(NotifyMessage::getTemplateType, reqVO.getTemplateType())
                .betweenIfPresent(NotifyMessage::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(NotifyMessage::getId));
    }

    default PageResult<NotifyMessage> selectPage(NotifyMessageMyPageReqVO reqVO, Long userId, Integer userType) {
        return selectPage(reqVO, new LambdaQueryWrapperX<NotifyMessage>()
                .eqIfPresent(NotifyMessage::getReadStatus, reqVO.getReadStatus())
                .betweenIfPresent(NotifyMessage::getCreateTime, reqVO.getCreateTime())
                .eq(NotifyMessage::getUserId, userId)
                .eq(NotifyMessage::getUserType, userType)
                .orderByDesc(NotifyMessage::getId));
    }

    default int updateListRead(Collection<Long> ids, Long userId, Integer userType) {
        return update(new NotifyMessage().setReadStatus(true).setReadTime(LocalDateTime.now()),
                new LambdaQueryWrapperX<NotifyMessage>()
                        .in(NotifyMessage::getId, ids)
                        .eq(NotifyMessage::getUserId, userId)
                        .eq(NotifyMessage::getUserType, userType)
                        .eq(NotifyMessage::getReadStatus, false));
    }

    default int updateListRead(Long userId, Integer userType) {
        return update(new NotifyMessage().setReadStatus(true).setReadTime(LocalDateTime.now()),
                new LambdaQueryWrapperX<NotifyMessage>()
                        .eq(NotifyMessage::getUserId, userId)
                        .eq(NotifyMessage::getUserType, userType)
                        .eq(NotifyMessage::getReadStatus, false));
    }

    default List<NotifyMessage> selectUnreadListByUserIdAndUserType(Long userId, Integer userType, Integer size) {
        return selectList(new QueryWrapperX<NotifyMessage>() // 由于要使用 limitN 语句，所以只能用 QueryWrapperX
                .eq("user_id", userId)
                .eq("user_type", userType)
                .eq("read_status", false)
                .orderByDesc("id").limitN(size));
    }

    default Long selectUnreadCountByUserIdAndUserType(Long userId, Integer userType) {
        return selectCount(new LambdaQueryWrapperX<NotifyMessage>()
                .eq(NotifyMessage::getReadStatus, false)
                .eq(NotifyMessage::getUserId, userId)
                .eq(NotifyMessage::getUserType, userType));
    }

}
